package org.example.classtopia.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Mapper.UserMapper;
import org.example.classtopia.Model.Entity.Schedule;
import org.example.classtopia.Model.Entity.User;
import org.example.classtopia.Model.dto.AiSessionCreateDTO;
import org.example.classtopia.Model.vo.AiSessionVO;
import org.example.classtopia.Service.AiChatSessionHistoryService;
import org.example.classtopia.Service.AiScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/aiSchedule")
@Tag(name = "AI行程安排")
public class AIScheduleController {

    @Autowired
    private AiScheduleService aiScheduleService;

    @Autowired
    private AiChatSessionHistoryService aiChatSessionHistoryService;
    @Autowired
    private UserMapper userMapper;

    @Operation(summary = "获取日程测试")
    @GetMapping("/getSchedule")
    public List<Schedule> getSchedule(@RequestParam("日期") LocalDate Date,
                                      @RequestParam("学期开始日期") String semester,
                                      @RequestParam("用户id") Long userId) {
        return aiScheduleService.getSchedules(Date, semester, userId);
    }

    @Operation(summary = "生成问题提示词测试")
    @PostMapping("/generate")
    public String getPrompt(@RequestParam("日期")  LocalDate Date,
                            @RequestParam(value = "学期开始日期" , defaultValue = "2025-06-23") String semester,
                            @RequestParam("用户id") Long userId){
        List<Schedule> schedules = aiScheduleService.getSchedules(Date, semester, userId);
        return aiScheduleService.generatePrompt(schedules, Date);
    }

    @Operation(summary = "AI生成测试")
    @PostMapping("/AIGenerate")
    public Result<?> AIGenerate(@RequestParam("日期") LocalDate Date,
                             @RequestParam(value = "学期开始日期", defaultValue = "2025-06-23") String semester,
                             @RequestParam("用户id") Long userId,
                             @RequestParam(value = "sessionId") String sessionId) {
        List<Schedule> schedules = aiScheduleService.getSchedules(Date, semester, userId);

        User user = userMapper.FindDataByID(Math.toIntExact(userId));

        if(user == null){
            return Result.error("用户不存在");
        }

        String userReply = "能告诉我今天的行程安排吗？";

        if(schedules.isEmpty()){
            String prompt = "复述“对不起，您今天没有日程哦~”这句话，其他一律不需要";
        
            return aiChatSessionHistoryService.AIgetChatResponse(prompt, sessionId, user, userReply);
        }

        String prompt = aiScheduleService.generatePrompt(schedules, Date);


        return aiChatSessionHistoryService.AIgetChatResponse(prompt, sessionId, user, userReply);
    }
}
